/*
 * @(#)AssADLText.java   10/02/14
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.editor.ass;

import fr.ema.arch3d.editor.mainGUI.BNFText.BNFText;
import fr.ema.arch3d.ref.Fractal;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextPane;
import javax.swing.text.Document;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class AssADLText {

    /** Field description */
    private JTextPane textPane;

    /**
     * Constructs ...
     *
     */
    public AssADLText() {
        textPane = new JTextPane();

        // textPane.setBackground(Color.black);
        textPane.setEditable(false);
    }

    /**
     * Method description
     *
     * @param container  
     * @return  
     */
    public static Document createTextAss(Component container) {
        BNFText doc = new BNFText();

        if (container != null) {

            // describe configuration name
            doc.insertBold("assembly");
            doc.insertSpace(1);

            try {
                doc.insert(Fractal.getAssemblyDescriptorController(container).getAssName());
            } catch (NoSuchInterfaceException e1) {

                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            doc.changeLine();
            doc.insertSpace(2);
            doc.insertBold("instance_of");

            // describe specification which this configuration implements
            doc.insertSpace(2);

            try {
                doc.insert(Fractal.getAssemblyDescriptorController(container).getConfiguration().getName());
            } catch (NoSuchInterfaceException e1) {

                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            doc.changeLine();

            List cmpIns_list = new ArrayList();
            List ctrIns_list = new ArrayList();

            try {
                cmpIns_list = Fractal.getAssemblyDescriptorController(container).getCmpsList();
                ctrIns_list = Fractal.getAssemblyDescriptorController(container).getCtrsList();

                // describe components
                doc.insertSpace(2);
                doc.insertBold("component_instances");
                doc.changeLine();

                for (int i = 0; i < cmpIns_list.size(); i++) {
                    doc.insertSpace(4);

                    String a[] = (String[]) cmpIns_list.get(i);

                    doc.insert(a[0]);

                    // pane.setBold("instanciates");
                    // pane.insertSpace(1);
                    // pane.insert(a[1]+";");
                    doc.insert(";");
                    doc.insertSpace(1);
                    doc.insertBold("as");
                    doc.insertSpace(1);
                    doc.insert(a[1] + ";");

                    // doc.insertBold("implements");
                    // doc.insertSpace(1);
                    // doc.insert(a[1]+";");
                    doc.changeLine();

                    // pane.changeLine();
                }

                // describe connectors
                doc.insertSpace(2);
                doc.insertBold("connector_instances");
                doc.changeLine();

                for (int i = 0; i < ctrIns_list.size(); i++) {
                    doc.insertSpace(4);

                    String a[] = (String[]) ctrIns_list.get(i);

                    doc.insert(a[0]);

                    // pane.setBold("instanciates");
                    // pane.insertSpace(1);
                    // pane.insert(a[1]+";");
                    doc.insert(";");
                    doc.insertSpace(1);

                    if ((i > 0) && (i + 1) % 2 == 0) {
                        doc.changeLine();
                    }

                    // pane.changeLine();
                }

                doc.changeLine();

                // describe connectors
            } catch (NoSuchInterfaceException e1) {

                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            doc.insertSpace(2);
            doc.insertBold("assembly_rules");
            doc.changeLine();
            doc.insertSpace(4);
            doc.insert("BikeTripC1.currency==\"Euro.\";");
            doc.insert("BikeCourseDBClassC1.company==BikeTripC1.company;");
            doc.insert("MaxInstanceNbr(BikeGUI)==8 && MinInstanceNbr(BikeGUI)==5");
            doc.changeLine();

            // version describe assembly
            doc.insertSpace(2);
            doc.insertBold("versionID");
            doc.insertSpace(1);
            doc.insert("1.0");
            doc.changeLine();

            return doc;
        }

        return null;
    }

    /**
     *
     * @param cnt: the connection which need to describe
     * @return the document which describe the connection using ADL grammar
     */
}
